package com.baizhi.controller;

import com.baizhi.annotation.LogAnnotation;
import com.baizhi.entity.User;
import com.baizhi.service.UserService;
import com.baizhi.service.UserThreadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private UserThreadService userThreadService;


    @RequestMapping("/selectAllUser")
    @ResponseBody
    @LogAnnotation(value = "查询全部用户信息",type = "select")
    public Map selectAllUser(int page,int limit,String userNickName){
        return userService.selectAllUser(page,limit,userNickName);
    }
    @LogAnnotation(value = "登录",type = "login")
    public String loginUser(String userName,String userPassword){
      boolean b=  userService.loginUser(userName,userPassword);
      if(b){
          return "";
      }else{
          return "";
      }
    }
    @RequestMapping("/insertUser")
    @ResponseBody
    @LogAnnotation(value = "添加用户信息",type = "insert")
    public Map insertUser(User user, MultipartFile file, HttpServletRequest request) throws IOException {
        System.out.println(user);
        System.out.println(file);
        String realPath=request.getServletContext().getRealPath("/user");//绝对路径
        System.out.println(realPath);
        //做重命名处理，防止文件同名被覆盖

        String newFileName=  new Date().getTime()+"_"+file.getOriginalFilename();
        user.setUserImage("/img/"+newFileName);
        file.transferTo(new File(realPath+"/"+newFileName));
        return userService.registerUser(user);

    }
    @RequestMapping("/selectBySex")
    @ResponseBody
    @LogAnnotation(value = "查询用户男女人数",type = "select")
    public List<Map>  selectBySex(){
        return userService.selectBySex();
    }


    @RequestMapping("/selectByProvince")
    @ResponseBody
    @LogAnnotation(value = "查询用户城市分布人数",type = "select")
    public List<Map> selectByProvince(){
        return userService.selectByProvince();
    }


    @RequestMapping("/selectByDayCount")
    @ResponseBody
    @LogAnnotation(value = "查询用户最近三周的注册量",type = "select")
     public  Map selectByDayCount(){
        return userService.selectByDayCount();
     }

    /**
     * 优化
     * @return
     */
    @RequestMapping("/selectAllCountThread")
    @ResponseBody
    @LogAnnotation(value = "查询优化后的用户信息统计",type = "select")
    public Map selectAllCountThread() throws Exception {
        long start = System.currentTimeMillis();
        Map map = userService.selectByThread();
        long end = System.currentTimeMillis();
        System.out.println(end-start+"ms");
        return map;

    }

    /**
     * 无优化
     * @return
     */
    @RequestMapping("/selectAllCount")
    @ResponseBody
    @LogAnnotation(value = "查询未优化的用户信息统计",type = "select")
    public Map selectAllCount() throws InterruptedException{
        long start = System.currentTimeMillis();
        Map map = userThreadService.selectAllCount();
        long end = System.currentTimeMillis();
        System.out.println(end-start+"ms");
        return map;
    }





}
